package com.gxa.modules.service.accessservice.impl;

import com.gxa.common.R;
import com.gxa.modules.mapper.accessmapper.AlarmRulesMapper;
import com.gxa.modules.mapper.accessmapper.ProjectInfoMapper;
import com.gxa.modules.pojo.ProjectInfo;
import com.gxa.modules.service.accessservice.ProjectInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ProjectInfoServiceImpl implements ProjectInfoService {

    @Autowired
    private ProjectInfoMapper projectInfoMapper;

    @Override
    public R selecProjectInfo(String projectName, String projectNumber, String projectStatusId,String page,String size) {
        R<Object> r = new R<>();

        int statrIndex=(Integer.parseInt(page)-1)*Integer.parseInt(size);
        int endIndex=Integer.parseInt(page)*Integer.parseInt(size);

        r.setCode(200);
        r.setMsg("查询成功！");
        r.setData(projectInfoMapper.selecProjectInfo(projectName,projectNumber,projectStatusId,statrIndex,endIndex));
        return r;
    }

    @Override
    public R insertProjectInfo(ProjectInfo projectInfo) {
        R<Object> r = new R<>();

        r.setCode(500);
        r.setMsg("新增失败！");

        if (projectInfoMapper.insertProjectInfo(projectInfo)>0){
            r.setCode(200);
            r.setMsg("新增成功！");
        }
        return r;
    }

    @Override
    public R deleteProjectInfo(String projectInfoId) {
        R<Object> r = new R<>();

        if ( projectInfoId ==null || "".equals(projectInfoId) ){
            r.setCode(500);
            r.setMsg("请选择要删除的信息！");
            return r;
        }

        r.setCode(500);
        r.setMsg("删除失败！");

        String[] strings = projectInfoId.split(",");
        for (int i=0;i<strings.length;i++) {
            projectInfoId=strings[i];
            if(projectInfoMapper.deleteProjectInfo(projectInfoId)>0){
                r.setCode(200);
                r.setMsg("删除成功！");
            }
        }


        return r;
    }

    @Override
    public R updateProjectInfo(ProjectInfo projectInfo) {
        R<Object> r = new R<>();

        r.setCode(500);
        r.setMsg("修改失败！");

        if (projectInfoMapper.updateProjectInfo(projectInfo)>0){
            r.setCode(200);
            r.setMsg("修改成功！");
        }
        return r;
    }
}
